package com.csdj.service.impl;

import com.csdj.dao.EmployeeDao;
import com.csdj.dao.impl.EmployeeDaoImpl;
import com.csdj.entity.Employee;
import com.csdj.entity.PageInfo;
import com.csdj.service.EmployeeService;
import com.csdj.util.MD5Util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * packageName com.csdj.service.impl
 *
 * @author winver
 * @version 1.0
 * @className EmployeeServiceImpl
 * @date 2024/11/27
 * @description TODO
 */

public class EmployeeServiceImpl implements EmployeeService {

    //构建数据访问层对象
    EmployeeDao employeeDao = new EmployeeDaoImpl();

    @Override
    public Map<String, Object> login(String name, String password) {
        //1.返回对象
        Map<String, Object> map = new HashMap<>();
        //2.调用接口 查询员工信息
        Employee employee = employeeDao.getEmployeeByName(name);
        String code = "0000";
        String msg = "登录成功";
        if(employee == null){
            code = "1001";
            msg = "用户名不存在";
        }else{
            //密码比较  [数据库存储的是加密的， 故此处比较时也需要加密]
            password = MD5Util.md5(password,5);
            if(!password.equals(employee.getPassword())){
                code = "1002";
                msg = "密码错误";
            }
        }
        map.put("code",code);
        map.put("msg",msg);
        map.put("emp",employee);
        return map;
    }

    @Override
    public Employee getEmployeeInfo(String name) {
        return employeeDao.getEmployeeInfoByName(name);
    }

    @Override
    public PageInfo<Employee> getPageEmp(int pageNum, int pageSize, String name, int depteId) {
        PageInfo<Employee> pageInfo = new PageInfo<>();
        pageInfo.setPageSize(pageSize);
        pageInfo.setCurrPageNo(pageNum);
        //查询总条数
        pageInfo.setTotals(employeeDao.getEmpsPageCount(name,depteId));
        int startIndex= (pageInfo.getCurrPageNo() - 1 ) * pageInfo.getPageSize();
        pageInfo.setList(employeeDao.getEmpsByPage(startIndex,pageInfo.getPageSize(),name,depteId));
        System.out.println("查询到的分页对象:" + pageInfo);
        return pageInfo;
    }

    @Override
    public int addEmp(Employee employee) {
        employee.setPassword(MD5Util.md5(employee.getPassword(),5));
        return employeeDao.addEmp(employee);
    }

    @Override
    public int getEmail(String email) {
        return employeeDao.getEmail(email);
    }

    @Override
    public List<Employee> getEmployeeInfoList(String name) {
        return employeeDao.getEmployeeInfoList(name);
    }
}
